package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.MaterialType;
import com.ruoyi.system.service.IMaterialTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 物料类型Controller
 * 
 * @author syn
 * @date 2024-01-09
 */
@RestController
@RequestMapping("/type")
public class MaterialTypeController extends BaseController
{
    @Autowired
    private IMaterialTypeService materialTypeService;

    /**
     * 查询物料类型列表
     */
    @RequiresPermissions("system:type:list")
    @GetMapping("/list")
    public AjaxResult list(MaterialType materialType)
    {
        List<TreeSelect> list = materialTypeService.selectMaterialTypeTreeList(materialType);
        System.out.println(list);
        return success(list);
    }

    /**
     * 导出物料类型列表
     */
    @RequiresPermissions("system:type:export")
    @Log(title = "物料类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialType materialType)
    {
        List<MaterialType> list = materialTypeService.selectMaterialTypeList(materialType);
        ExcelUtil<MaterialType> util = new ExcelUtil<MaterialType>(MaterialType.class);
        util.exportExcel(response, list, "物料类型数据");
    }

    /**
     * 获取物料类型详细信息
     */
    @RequiresPermissions("system:type:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(materialTypeService.selectMaterialTypeById(id));
    }

    /**
     * 新增物料类型
     */
    @RequiresPermissions("system:type:add")
    @Log(title = "物料类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialType materialType)
    {
        return toAjax(materialTypeService.insertMaterialType(materialType));
    }

    /**
     * 修改物料类型
     */
    @RequiresPermissions("system:type:edit")
    @Log(title = "物料类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialType materialType)
    {
        return toAjax(materialTypeService.updateMaterialType(materialType));
    }

    /**
     * 删除物料类型
     */
    @RequiresPermissions("system:type:remove")
    @Log(title = "物料类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(materialTypeService.deleteMaterialTypeByIds(ids));
    }
}
